package com.mhxy.service.impl;

import com.mhxy.constant.PicConstants;
import com.mhxy.enums.Coord;
import com.mhxy.service.AbstractTask;
import com.mhxy.util.FuzzyPicUtil;
import com.mhxy.util.MouseUtil;
import com.mhxy.util.ThreadUtil;
import com.sun.jna.platform.win32.WinDef;
import lombok.extern.slf4j.Slf4j;

/**
 * 回家睡觉
 */
@Slf4j
public class GoHomeSleepTaskImpl extends AbstractTask {

    public GoHomeSleepTaskImpl(WinDef.HWND hwnd_0) {
        super(hwnd_0);
    }

    @Override
    public Boolean before() throws Exception {
        clickEsc1();
        if (FuzzyPicUtil.findPic(hwnd_0, PicConstants.公共_下面的活动开关, 1).isFinded()) {
            return true;
        }
        return goToJianYe();
    }

    @Override
    public Boolean execute() throws Exception {
        FuzzyPicUtil.findAndClickRandom(hwnd_0, PicConstants.公共_下面的活动开关, 3);

        ThreadUtil.sleep1();
        if (!FuzzyPicUtil.findAndClickRandom(hwnd_0, PicConstants.公共_家园, 5)) {
            log.error("【{}】没有找到“家园”按钮！", getTaskName());
            return false;
        }

        ThreadUtil.sleep2();
        if (!FuzzyPicUtil.findAndClickRandom(hwnd_0, PicConstants.公共_家园_回家, 5)) {
            log.error("【{}】没有找到“回家”按钮！", getTaskName());
            return false;
        }

        ThreadUtil.sleep3();
        if (!FuzzyPicUtil.findAndClickRandom(hwnd_0, PicConstants.公共_家园_进入房子, 5)) {
            log.error("【{}】没有找到“进入房子”！", getTaskName());
            return false;
        }

        ThreadUtil.sleep3();
        if (FuzzyPicUtil.findAndClickRandom(hwnd_0, PicConstants.公共_家园_打理, 3)) {
            if (FuzzyPicUtil.findAndClickRandom(hwnd_0, PicConstants.公共_家园_打理_大扫除, 3)) {
                FuzzyPicUtil.findAndClickRandom(hwnd_0, PicConstants.公共_家园_打理_大扫除_开始, 3);
            }
        }
        clickEsc3();

        ThreadUtil.sleep2();
        pic = FuzzyPicUtil.findPic(hwnd_0, PicConstants.公共_家园_管家, 5, 0.8);
        if (!pic.isFinded()) {
            log.error("【{}】没有找到“管家”！", getTaskName());
            return false;
        }
        MouseUtil.lClickRandom(hwnd_0, pic.getPicX()+ Coord.FIRMLY_STEWARD_XY.getX(), pic.getPicY()-Coord.FIRMLY_STEWARD_XY.getY());

        ThreadUtil.sleep3();
        pic = FuzzyPicUtil.findPic(hwnd_0, PicConstants.公共_家园_卧室休息, 5);
        if (!pic.isFinded()) {
            log.error("【{}】没有找到“卧室休息”！", getTaskName());
            return false;
        }
        MouseUtil.lClickRandom(hwnd_0, pic.getPicX()+ Coord.FIRMLY_REST_XY.getX(), pic.getPicY()+Coord.FIRMLY_REST_XY.getY());

        ThreadUtil.sleep(5000);
        pic = FuzzyPicUtil.findPic(hwnd_0, PicConstants.公共_家园_卧室休息按钮, 5);
        if (!pic.isFinded()) {
            log.info("【{}】今日已经休息过了！", getTaskName());
            return true;
        }
        for (int i = 0; i < 5; i++) {
            MouseUtil.lClickRandom(hwnd_0, pic.getPicX(), pic.getPicY());
        }

        return true;
    }

    @Override
    public Boolean after() throws Exception {
        clickEsc3();
        return true;
    }

    @Override
    public String getTaskName() throws Exception {
        return "回家睡觉";
    }
}
